package com.huluobo.jetpackdemo

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 *  Created by LC on 2024/12/10.
 */
class TipDialog(context: Context) : Dialog(context), LifecycleObserver {
    init {
        if (context is AppCompatActivity) {
            context.lifecycle.addObserver(this)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.view_dialog)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private fun onShow() {
        show()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDismiss() {
        if (isShowing) {
            dismiss()
        }
    }
}